<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title id="role_manager">角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--头部工具栏区域-->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>
        <!--表格区域-->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <!--行工具栏-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <!--添加修改窗口弹出层-->
        <div style="display: none;padding:5px" id="addOrUpdateWindow">
            <form class="layui-form layuimini-form " id="dataFrm" lay-filter="dataFrm">
                <!--主键隐藏域，保存主键-->
                <input type="hidden" name="rId" />
                <div class="layui-form-item">
                    <label class="layui-form-label required">角色编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="rCode" lay-verify="required" lay-reqtext="角色编码不能为空" placeholder="请输入角色编码(前缀ROLE_)" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">角色名</label>
                    <div class="layui-input-block">
                        <input type="text" name="rName" lay-verify="required" lay-reqtext="角色名不能为空" placeholder="请输入角色名" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色描述</label>
                    <div class="layui-input-block">
                        <input type="textarea" name="rDesc"  placeholder="请输入角色描述" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="../../static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;
        //数据表格
        var tableIns = table.render({
            elem: '#currentTableId',
            url: '/role/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'rId',  title: 'ID', sort: true},
                {field: 'rCode',  title: '角色编码',edit:true},
                {field: 'rName',  title: '角色名称', sort: true},
                {field: 'rDesc',  title: '角色描述', sort: true},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            done: function(res,curr,count){ //解决当一页的最后一个数据被删除后，不会跳转到前一页的问题
                //判断当前页码是否大于1且当前页数据量为0
                if(curr > 1 && res.data.length == 0){
                    var pageValue = curr - 1;
                    //刷新数据表格的数据
                    tableIns.reload({
                        page: {curr:pageValue}
                    })
                }
            }
        });


        /**
         * toolbar监听头部工具栏事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                openAddWindow();
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                groupDel(data);
            }

        });

        function groupDel(data) {
            //批量删除角色
        }
        //保存提交地址和窗口的索引
        var url,mainIndex;
        /**
         * 打开添加窗口
         */
        function openAddWindow(){
            mainIndex = layer.open({
                title: '添加类型',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['40%', '80%'],   //宽，高
                content: $("#addOrUpdateWindow"),   //引用的内容窗口
                success: function () {
                    //提交地址
                    url = "/role/add";
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                }
            })
        }
        //监听表单提交事件(负责添加和编辑)
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            //发送请求
            $.post(url,data.field,function (result) {
                if (result.success){
                    //成功
                    layer.alert(result.message,{icon:1});
                    //刷新当前数据表格
                    tableIns.reload();
                    //关闭当前窗口
                    layer.close(mainIndex);
                }else {
                    //失败
                    layer.alert(result.message,{icon:2});
                }
            },"json");
            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
        //监听行工具栏事件（编辑与单项删除）
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openUpdateView(data);
                return false;
            } else if (obj.event === 'delete') {
                deleteById(data);
            }
        });
        //打开更新窗口
        function openUpdateView(data) {
            mainIndex = layer.open({
                title: '修改角色',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['40%', '80%'],   //宽，高
                content: $("#addOrUpdateWindow"),   //引用的内容窗口
                success: function () {
                    //提交地址
                    url = "/role/update";
                    //表单数据回显
                    form.val("dataFrm",data);
                }
            })
        }
        //删除角色
        function deleteById(data) {
            //发送请求查询该角色下是否存在用户信息
            $.get("/role/checkRoleHasUser",{"rId":data.rId},function (result) {
                if (result.exist){
                    layer.alert(result.message,{icon:0});
                }else {
                    //提示用户是否确认删除
                    layer.confirm('确定要删除[<span style="color: red">'+data.rName+'</span>]吗？',{icon:3,title:"提示"}, function (index) {
                        // obj.del();
                        //发送删除请求
                        $.post("/role/deleteById", {"rId":data.rId},function (result) {
                            if (result.success){
                                //成功
                                layer.alert(result.message,{icon:1});
                                //刷新当前数据表格
                                tableIns.reload();
                            }else{
                                //提示
                                layer.alert(result.message,{icon:2})
                            }
                        },"json");
                        layer.close(index);
                    });
                }
            },"json")
        }
    });
</script>

</body>
</html>